module;
#include <QObject>
#include <QDebug>
#include <QTcpServer>
#include <QTcpSocket>
#include <QList>
#include <QtCore/qtmochelpers.h>
module XYtcpSocket;

//Client
TcpSocketClient::TcpSocketClient(QObject *parent)
    : QObject(parent), m_socket(new QTcpSocket(this))
{
    connect(m_socket, &QTcpSocket::connected, this, &TcpSocketClient::onConnected);
    connect(m_socket, &QTcpSocket::disconnected, this, &TcpSocketClient::onDisconnected);
    connect(m_socket, &QTcpSocket::readyRead, this, &TcpSocketClient::onReadyRead);
}

TcpSocketClient::~TcpSocketClient()
{
    m_socket->disconnectFromHost();
}

void TcpSocketClient::connectToServer(const QString &address, quint16 port) {
    m_socket->connectToHost(address, port);
    qDebug() << "正在连接服务器"<<address<<port;
}

void TcpSocketClient::sendData(const QByteArray &data) {
    if (m_socket->state() == QAbstractSocket::ConnectedState) {
        m_socket->write(data);
    } else {
        qDebug() << "Not connected to server."<<m_socket->state()<<m_socket->error();
    }
}

void TcpSocketClient::disconnect() {
    m_socket->disconnectFromHost();
}

void TcpSocketClient::connectToServerSlot(const QString &address, quint16 port) {
    TcpSocketClient::connectToServer(address, port);
}

void TcpSocketClient::sendDataSlot(const QByteArray &data) {
    TcpSocketClient::sendData(data);
}

void TcpSocketClient::disconnectSlot() {
    m_socket->disconnectFromHost();
}
void TcpSocketClient::onReadyRead()
{
    QByteArray data = m_socket->readAll();
    emit dataReceived(data);
}

void TcpSocketClient::onConnected()
{
    emit connected();
    qDebug() << "已经连接服务器";
}

void TcpSocketClient::onDisconnected()
{
    emit disconnected();
}
//Server
TcpSocketServer::TcpSocketServer(QObject *parent)
    : QObject(parent), m_server(new QTcpServer(this))
{
    connect(m_server, &QTcpServer::newConnection, this, &TcpSocketServer::onNewConnection);
}

TcpSocketServer::~TcpSocketServer()
{
    disconnectAll();
}

void TcpSocketServer::startServer(quint16 port)
{
    if (!m_server->listen(QHostAddress::Any, port)) {
        qDebug() << "Server could not start!";
    } else {
        qDebug() << "Server started on port" << port;
    }
}

void TcpSocketServer::sendDataToAll(const QByteArray &data)
{
    for (QTcpSocket *clientSocket : m_clientSockets) {
        if (clientSocket->state() == QAbstractSocket::ConnectedState) {
            clientSocket->write(data);
        } else {
            qDebug() << "Client not connected.";
        }
    }
}

void TcpSocketServer::disconnectAll()
{
    for (QTcpSocket *clientSocket : m_clientSockets) {
        clientSocket->disconnectFromHost();
        clientSocket->deleteLater();
    }
    m_clientSockets.clear();
    m_server->close();
}

void TcpSocketServer::startServerSlot(quint16 port) {
    TcpSocketServer::startServer(port);
}

void TcpSocketServer::sendDataToAllSlot(const QByteArray &data) {
    TcpSocketServer::sendDataToAll(data);
}

void TcpSocketServer::disconnectAllSlot() {
    TcpSocketServer::disconnectAll();
}

void TcpSocketServer::onNewConnection()
{
    QTcpSocket *clientSocket = m_server->nextPendingConnection();
    m_clientSockets.append(clientSocket);

    connect(clientSocket, &QTcpSocket::readyRead, this, &TcpSocketServer::onReadyRead);
    connect(clientSocket, &QTcpSocket::disconnected, this, &TcpSocketServer::onDisconnected);

    emit newConnection();
}

void TcpSocketServer::onReadyRead()
{
    QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
    if (clientSocket) {
        QByteArray data = clientSocket->readAll();
        emit dataReceived(data);
    }
}

void TcpSocketServer::onDisconnected()
{
    QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(sender());
    if (clientSocket) {
        m_clientSockets.removeAll(clientSocket);
        clientSocket->deleteLater();
        emit disconnected();
    }
}


//放在文件最后，文件需要手动生成
#include "XYtcpSocket.moc"
